#!/bin/bash

# Author : wzz

option="${1}"
case ${option} in
    'push')
        tagStr=`git tag | tail -1`
        # 拆分字符串
        IFS_old=$IFS
        IFS='v' tagArr=($tagStr)
        IFS='.' arr=(${tagArr[1]})
        IFS=$IFS_old
        # tag+1
        if [ ${arr[2]} == 9 ];then
            arr[2]=0
            if [ ${arr[1]} == 9 ]; then
                arr[1]=0
                let arr[0]++
            else
                let arr[1]++
            fi
        else
            let arr[2]++
        fi
        newTag="${tagArr[0]}v${arr[0]}.${arr[1]}.${arr[2]}"
        echo `git tag $newTag & git push & git push --tags`
    ;;
    'pull')

        oldTagStr=`git tag | tail -1`
        echo `git fetch --tags`
        tagStr=`git tag | tail -1`
        if [ $oldTagStr != $tagStr ];then
            echo `git checkout ${tagStr}`
        else
            echo "not found new tag!">&2
        fi
    ;;
    'autoPull')
        echo `sudo -u root -S git checkout . < /bdata/sudopass.secret`
        echo `sudo -u root -S git pull < /bdata/sudopass.secret`
    ;;
    *)
#        exit 'option error!'
        echo "option error!">&2
    ;;
esac